package com.engineering.elink.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.SkillCo;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.domain.KeyWord;
import com.engineering.elink.domain.VideoTopCategory;
import com.engineering.elink.domain.skill.Skill;
import com.engineering.elink.domain.skill.SkillBrowse;
import com.engineering.elink.service.SkillService;
import com.engineering.elink.skilldao.SkillBrowseMapper;
import com.engineering.elink.skilldao.SkillFileMapper;
import com.engineering.elink.skilldao.SkillMapper;
import com.engineering.elink.skilldao.SkillVideoMapper;
import com.engineering.elink.util.CollectionUtil;
import com.engineering.elink.util.ResJsonUtil;
import com.engineering.elink.videodao.KeyWordMapper;
import com.engineering.elink.videodao.LabelMapper;
import com.engineering.elink.videodao.VideoTopCategoryMapper;
import com.engineering.elink.vo.EvaluateSkillVo;
import com.engineering.elink.vo.SkillVo;

@Service
public class SkillServiceImpl implements SkillService{

	@Autowired
	private KeyWordMapper keyWordMapper;
	
	@Autowired
	private SkillMapper skillMapper;
	
	@Autowired
	private LabelMapper labelMapper;
	
	@Autowired
	private VideoTopCategoryMapper videoTopCategoryMapper; 
	
	@Autowired
	private SkillFileMapper skillFileMapper;
	
	@Autowired
	private SkillBrowseMapper skillBrowseMapper;
	
	@Autowired
	private SkillVideoMapper skillVideoMapper;

	@Override
	public Map<String, Object> getKeyWord(int count) {
		//工程技能默认取为1的
		return ResJsonUtil.getMap(ErrorType.SUCCESS,keyWordMapper.getKeyWord(count,1));
		
	}


	@Override
	public Map<String, Object> getSkillsList(SkillCo co,Integer count) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<SkillVo> list = skillMapper.selectList(co);
		for (SkillVo vo : list) {
			vo.setLabelList(labelMapper.getLabelList(vo.getLabelIds()));
			
			BaseCo bco=new BaseCo();
		
			bco.setPageSize(count);
			
			//加载文件资料
			vo.setSkillFileVoList(skillFileMapper.selectFileBySkillId(vo.getSkillId(),bco));
			
		}
		
		map.put("list", list);
		map.put("Pagination", new Pagination(co, skillMapper.selectCount(co)));

		VideoTopCategory vt=getSkillTop(co.getTcId());
		if(vt!=null){
			// 将关键词存入表中
			KeyWord kw = keyWordMapper.selectByKeyWord(vt.getTcName(),1);
			
			if (kw != null) {// 表示该关键词存在
				kw.setSearchCount(kw.getSearchCount() + 1);
				keyWordMapper.updateByPrimaryKeySelective(kw);
			} else {
				if (StringUtils.isNotEmpty(vt.getTcName())) {
					keyWordMapper.insertSelective(new KeyWord(vt.getTcName(),1,1));
				}
			}
		}
		
		return map;
	}

	@Override
	public Map<String, Object> getDownLoadRank(int count) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		BaseCo bco=new BaseCo();
		//bco.setAll(true);
		bco.setPageSize(count);
		
		map.put("list", skillMapper.getDownLoadRank(bco));
		
		return map;
		
	}
	

	@Override
	public Map<String, Object> get(Integer skillId, String userId) {
		EvaluateSkillVo vo = skillMapper.selectInfo(skillId);
		if (vo != null) {
			vo.setLabelList(labelMapper.getLabelList(vo.getLabelIds()));

			//往技能浏览表插入数据
			skillBrowseMapper.insertSelective(new SkillBrowse(skillId, userId));
			
			//更新技能浏览次数
			Skill skill=skillMapper.selectByPrimaryKey(skillId);
			skill.setVisitCount(skill.getVisitCount()+1);
			skillMapper.updateByPrimaryKeySelective(skill);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("skill", vo);
			//查询该技能的分类
			map.put("category", getSkillTop(vo.getTcId()));
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS, map);

		} else {
			return ResJsonUtil.getMap(ErrorType.SKILL_ERROR);
		}
	}
	
	private VideoTopCategory getSkillTop(Integer tcId){
		List<VideoTopCategory> vtcList=skillMapper.selectSkillTopList(tcId);
		if(CollectionUtil.collectionIsNotEmpty(vtcList)){
			return vtcList.get(0);
		}
		return null;
	}

	@Override
	public Map<String, Object> getSigleSkillFile(int skillId, int count) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		BaseCo bco=new BaseCo();
		//bco.setAll(true);
		bco.setPageSize(count);
		
		map.put("list", skillFileMapper.selectFileBySkillId(skillId,bco));
		
		return map;
	}
	

	@Override
	public Map<String, Object> getSigleSkillVideo(int skillId, int count) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		BaseCo bco=new BaseCo();
		//bco.setAll(true);
		bco.setPageSize(count);
		
		map.put("list", skillVideoMapper.selectVideoBySkillId(skillId,bco));
		
		return map;
	}
	

	@Override
	public Map<String, Object> getSameSkillFile(int skillId, int count) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		BaseCo bco=new BaseCo();
		//bco.setAll(true);
		bco.setPageSize(count);
		
		map.put("list", skillFileMapper.selectSameFileBySkillId(skillId,bco));
		
		return map;
	}

	@Override
	public Map<String, Object> getEnginSkillFile(Integer tcId, Integer count) {
		return ResJsonUtil.getMap(ErrorType.SUCCESS,skillFileMapper.selectFileByTcId(tcId, 2, count));
	}

	@Override
	public Map<String, Object> getSameSkill(BaseCo co, Integer skillId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", skillMapper.getSameSkill(co,skillId));
		map.put("Pagination", new Pagination(co, skillMapper.countSameSkill(skillId)));
		
		return map;
	}
	
}
